package com.company.project.command.warehouse;

import com.company.project.common.exception.BusinessException;
import com.company.project.common.exception.handler.RestExceptionHandler;
import com.company.project.exception.NormalException;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Data
public class WarehouseCreateorUpdateCommand {
    /**
     * 仓库名称
     */
    @NotEmpty(message = "仓库名称不能为空")
    private String name;

    /**
     * 所属中心
     */
    @NotEmpty(message = "所属中心不能为空")
    private String owner;

    /**
     * 仓库属性
     */
    @NotEmpty(message = "仓库属性不能为空")
    private String category;

    /**
     * 经办人
     */
    private String createdBy;

    @CreatedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @LastModifiedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastModifiedDate;

    public void validData(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (ObjectError oe : bindingResult.getAllErrors()) {
                stringBuilder.append(oe.getDefaultMessage() + "\n");
            }
            throw new BusinessException(stringBuilder.toString());
        }
    }
}


